//
//  NewDeviceView.m
//  YSSmartHome
//
//  Created by yanghao on 2016/9/30.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "NewDeviceView.h"
#import "ZTDevice.h"
#import "SureView.h"
#import "DeviceEditController.h"
#import "UIView+Frame.h"
@interface NewDeviceView ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)UITableView *tableView;

@end
@implementation NewDeviceView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    UIButton *titleBtn = [[UIButton alloc]init];
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [titleBtn setImage:[UIImage imageNamed:@"newdevice_icon_find"] forState:UIControlStateNormal];
    [titleBtn setTitle:@"发现新设备" forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:titleBtn];
    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.equalTo(self);
        make.leading.equalTo(self).offset(10);
        make.height.equalTo(55);
        
    }];
    
    UIView *splitView = [[UIView alloc]init];
    splitView.backgroundColor = RGBColor(233, 233, 233);
    [self addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.height.equalTo(1);
        make.top.equalTo(titleBtn.bottom);
    }];
   
    SureView *sureView = [[SureView alloc]init];
    sureView.backgroundColor = [UIColor whiteColor];
    __weak typeof(self)weakSelf = self;
    sureView.sureBtnClickBlock = ^(){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if (strongSelf.sureBloc) {
            strongSelf.sureBloc();
        }    
    };
    [self addSubview:sureView];
    [sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.height.equalTo(55);
    }];
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.top.equalTo(splitView.bottom);
        make.bottom.equalTo(sureView.top);
        
    }];
    
    
}

- (void)addDev:(ZTDevice *)dev
{
    BOOL flage = false;
    for (ZTDevice *device in self.datasource) {
        if (device.apsn == dev.apsn && device.luid == dev.luid) {
            flage = true;
            break;
        }
    }
    if (!flage) {
        [self.datasource addObject:dev];
        [self.tableView reloadData];
    }
}
- (void)edit {
    debugLogInfo(@"--------");
}

- (UIImage *)loadImage:(ZTDevice *)dev
{
    switch (dev.devType) {
        case ZTDeviceTypeOneWay0Switch:
        case ZTDeviceTypeOneWaySwitch:
        case ZTDeviceTypeTwoWay0Switch:
        case ZTDeviceTypeTwoWaySwitch:
        case ZTDeviceTypeThreeWay0Switch:
        case ZTDeviceTypeThreeWaySwitch:
        case ZTDeviceTypeFourWay0Switch:
        case ZTDeviceTypeFourWaySwitch:
            return [UIImage imageNamed:@"powerSocketOpen"];
            break;
        case ZTDeviceTypeZTDoorsensor:
            return [UIImage imageNamed:@"ZTDoorsensorOpen"];
            break;
        case ZTDeviceTypeLCCamera:
            return [UIImage imageNamed:@"home_camera_online"];
            break;
        case ZTDeviceTypeOneWaySocket:
            return [UIImage imageNamed:@"home_light_open"];
            
            break;
        default:
            break;
    }
    return nil;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   
    ZTDevice *device = (ZTDevice *)self.datasource[indexPath.item];
    cell.textLabel.text = device.name;
    UIButton *accesView = [[UIButton alloc]init ];
    [accesView setBackgroundImage:[UIImage imageNamed:@"newdevice_icon_edit"] forState:UIControlStateNormal];
    [accesView addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    accesView.yh_size = CGSizeMake(16, 16);
    cell.accessoryView = accesView;
    cell.imageView.image = [self loadImage:device];
    cell.imageView.yh_size =  CGSizeMake(50, 50);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDlegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DeviceEditController *deviceEditVc = [[DeviceEditController alloc]init];
    UIViewController *vc = [self currentViewController];
    UINavigationController *nav = [(UITabBarController *)vc selectedViewController];
    deviceEditVc.devListView = self;
    deviceEditVc.device = self.datasource[indexPath.row];
    __weak typeof(self)weakSelf = self;
    deviceEditVc.setDevInfoBlcok = ^(ZTDevice *dev){
        __strong typeof(weakSelf) strongSelf = self;
        if (!strongSelf) {
            return ;
        }
        strongSelf.datasource[indexPath.row] = dev;
        [strongSelf.tableView reloadData];
    };
    self.hidden = YES;
    [nav pushViewController:deviceEditVc animated:YES];
}

- (UIViewController *)currentViewController
{
    UIWindow *keyWindow  = [UIApplication sharedApplication].keyWindow;
    UIViewController *vc = keyWindow.rootViewController;
    while (vc.presentedViewController)
    {
        vc = vc.presentedViewController;
        if ([vc isKindOfClass:[UINavigationController class]])
        {
            vc = [(UINavigationController *)vc visibleViewController];
        }
        else if ([vc isKindOfClass:[UITabBarController class]])
        {
            vc = [(UITabBarController *)vc selectedViewController];
        }
    }
    return vc;
}

- (UINavigationController *)currentNavigationController
{
    UIViewController *currentVc = [self currentNavigationController];
    if ([currentVc isKindOfClass:[UITabBarController class]]) {
        return [(UITabBarController *)currentVc selectedViewController];
    } else {
        return  [currentVc navigationController];
    }
}


#pragma mark - lazy loading

- (UITableView *)tableView
{
    if (!_tableView ) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.rowHeight = 55.f;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
    
}

- (NSMutableArray *)datasource
{
    if (!_datasource) {
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}
@end
